package com.biostime.springdraw.modules.sd.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;
import com.biostime.springdraw.modules.sd.entity.Award;
import com.biostime.springdraw.modules.sd.entity.Winner;
import com.biostime.springdraw.modules.sd.service.AwardService;
import com.biostime.springdraw.modules.sd.service.ExportService;
import com.biostime.springdraw.modules.sd.service.WinnerService;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 中奖表Controller
 * @author king
 * @version 2016-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/winner")
public class WinnerController extends BaseController {

	@Autowired
	private WinnerService WinnerService;
	
	@Autowired
	private ExportService exportService;
	
	@Autowired
	private AwardService awardService;
	
	@ModelAttribute
	public Winner get(@RequestParam(required=false) String id) {
		Winner entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = WinnerService.get(id);
		}
		if (entity == null){
			entity = new Winner();
		}
		return entity;
	}

	@RequiresPermissions("sd:section:view")
	@RequestMapping(value = {"list", ""})
	public String list(Winner Winner, HttpServletRequest request, HttpServletResponse response, Model model) {
		WinnerService.recoveryIntimeAward();
		Page<Winner> page = WinnerService.findPage(new Page<Winner>(request, response), Winner); 
		List<Winner> list = page.getList();
		
		//遍历winner,如果是分享抽奖,并且是非会员,则设置过期状态
		if(list != null && !list.isEmpty()){
			Winner w = null;
			for(int i = 0;i<list.size() ; i++){
				w = list.get(i);
				if(w.getSource().equals("share")){ 					
					if(w.getIsLimit() ==1){
						list.get(i).setIs_overdue(1); 
					}else{
						if(w.getMember().getId().equals("0")){ 
							list.get(i).setIs_overdue(2); 
						}
					}							
				}
			}
		}
		page.setList(list);
		model.addAttribute("page", page);
		return "modules/sd/WinnerList";
	}

	@RequiresPermissions("sd:section:view")
	@RequestMapping(value = "form")
	public String form(Winner Winner, Model model) {
		List<Award> list = awardService.findList(null);
		model.addAttribute("Winner", Winner);
		model.addAttribute("awardList", list);		
		return "modules/sd/WinnerForm";
	}

	@RequiresPermissions("sd:section:edit")
	@RequestMapping(value = "save")
	public String save(Winner Winner, Model model, RedirectAttributes redirectAttributes) {
		Winner.setCreateBy(new User("0"));
		Winner.setUpdateDate(new Date());
		Winner.setUpdateBy(new User("0"));
		Winner.setCreateDate(new Date()); 
		Award award = awardService.get(String.valueOf(Winner.getAwardId()));
		if(award !=null){
			Winner.setAwardname(award.getName()); 
		}
		if (!beanValidator(model, Winner)){
			return form(Winner, model);
		}
		WinnerService.save(Winner);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/winner/?repage&isAward=1&isLimit=2";
	}

	@RequiresPermissions("sd:section:delete")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Winner Winner, RedirectAttributes redirectAttributes) {	
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				WinnerService.delete(new Winner(id));
			}
		}else{
			WinnerService.delete(Winner);
		}		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/winner/?repage&isAward=1&isLimit=2";
	}
	
	@RequestMapping("export")
	public void exportWinner(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws Exception {			
		LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
		ArrayList<LinkedHashMap<String,Object>>  list = WinnerService.exportWinner();
		list.add(map);
		exportService.crateTempFile(list,request,response); 	
		addMessage(redirectAttributes, "导出数据成功");
	}	
	
}